package com.github.postsanf.yinian.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.TimeLineStatusFristAdapter;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNStatusResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.StringUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class TimeLineStatusFristSearchActivity extends BaseActivity implements View.OnClickListener {
    private YNAlbum curAlbum;
    private TextView search_tv;
    private EditText labET;
    private ImageView labClear;
    private RecyclerView mRecyclerView;
    private ImageView search_empty;
    private TimeLineStatusFristAdapter mTimeLineStatusAdapter;
    private List<YNStatus> groupStatusItems = new ArrayList<>();
    private int groupid;
    private InputMethodManager imm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timeline_topstatussearch);
        curAlbum = (YNAlbum) getIntent().getSerializableExtra("StatusFrist");
        groupid = curAlbum.getGroupid();
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        initView();
    }


    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        search_tv = (TextView) findViewById(R.id.search_tv);
        search_tv.setOnClickListener(this);
        labET = (EditText) findViewById(R.id.publabel_edit);
//        labET.setFocusable(true);
//        labET.setFocusableInTouchMode(true);
//        labET.requestFocus();
//        new Timer().schedule(new TimerTask() {
//                           public void run() {
//                               InputMethodManager inputManager =
//                                       (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                               inputManager.showSoftInput(labET, 0);
//                           }
//                       },
//                200);
        labClear = (ImageView) findViewById(R.id.publabel_clear);
        labClear.setOnClickListener(this);
        labET.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.length() > 0) {
                    labClear.setVisibility(View.VISIBLE);
                } else {
                    labClear.setVisibility(View.INVISIBLE);
                }
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });
        mRecyclerView = (RecyclerView) this.findViewById(R.id.rv_edit_search);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        search_empty = (ImageView) this.findViewById(R.id.search_empty);
        mTimeLineStatusAdapter = new TimeLineStatusFristAdapter(TimeLineStatusFristSearchActivity.this, groupStatusItems, application.getCurID(), true);
        mRecyclerView.setAdapter(mTimeLineStatusAdapter);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.publabel_clear:
                labET.setText("");
                break;
            case R.id.search_tv:
                String searchTx = labET.getText().toString();
                if (StringUtils.isEmpty(searchTx)) {
                    Toast.makeText(this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                noticeServiceInit(searchTx);
                break;
        }
    }

    private void noticeServiceInit(String nickname) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_NICKNAME, nickname);
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupid));
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/activity/SearchSpaceUserEvent")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Log.i("xjs", "SearchSpaceUserEvent===>" + s);
                        YNStatusResponse resAlbumStatus = gson.fromJson(s, YNStatusResponse.class);
                        if (resAlbumStatus != null && ReqUtils.isSuccess(resAlbumStatus.getCode())) {
                            List<YNStatus> datas = resAlbumStatus.getData();
                            if (datas != null && datas.size() > 0) {
                                mRecyclerView.setVisibility(View.VISIBLE);
                                search_empty.setVisibility(View.GONE);
                                mTimeLineStatusAdapter.addItemsFromTop(datas);
                                mRecyclerView.scrollToPosition(0);
                                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                            } else {
                                Toast.makeText(TimeLineStatusFristSearchActivity.this, "抱歉，没有相关内容!", Toast.LENGTH_SHORT).show();
                                search_empty.setVisibility(View.VISIBLE);
                                mRecyclerView.setVisibility(View.GONE);
                            }

                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }
}
